﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitTools;
/// <summary>
/// 战斗状态-准备
/// </summary>
public class BattleState_Prepare : IState
{
    private Camera mainCamera;
    private Character dragCharacter;
    private Grid curGrid;

    async void IState.OnEnter(StateMechine mechine)
    {
        mainCamera = Camera.main;
        await UIManager.Instance.OpenUI<BattlePreparePanel>();
        UIManager.Instance.CloseUI<LoadingPanel>();
        MessageManager.Instance.RegisterMessage(Message.BattlePreparePanel_PointDownArea, OnAreaPointDown);
        MessageManager.Instance.RegisterMessage(Message.BattlePreparePanel_PointUpArea, OnAreaPointUp);
        MessageManager.Instance.RegisterMessage(Message.BattlePreparePanel_ClickArea, OnAreaClick);
        MessageManager.Instance.RegisterMessage(Message.BattlePreparePanel_BeginDragArea, OnAreaBeginDrag);
        MessageManager.Instance.RegisterMessage(Message.BattlePreparePanel_DragArea, OnAreaDrag);
        MessageManager.Instance.RegisterMessage(Message.BattlePreparePanel_EndDragArea, OnAreaEndDrag);
    }

    void IState.OnExit(StateMechine mechine)
    {
        
    }

    void IState.OnUpdate(StateMechine mechine)
    {
        
    }

    #region 消息监听

    private void OnAreaPointDown(object[] args)
    {

    }

    private void OnAreaPointUp(object[] args)
    {

    }

    private void OnAreaClick(object[] args)
    {
        Vector2 pos = (Vector2)args[0];
        var wordpos = mainCamera.ScreenToWorldPoint(pos);
        var characters = BattleManager.Instance.Characters;
        for (int i = 0; i < characters.Count; i++)
        {
            bool overlap = characters[i].Collider.OverlapPoint(wordpos);
            if (overlap)
            {
                //预览攻击范围
                break;
            }
        }
    }

    private void OnAreaBeginDrag(object[] args)
    {
        Vector2 pos = (Vector2)args[0];
        var wordpos = mainCamera.ScreenToWorldPoint(pos);
        var characterList = BattleManager.Instance.Characters;
        for (int i = 0; i < characterList.Count; i++)
        {
            bool overlap = characterList[i].Collider.OverlapPoint(wordpos);
            if (overlap)
            {
                dragCharacter = characterList[i];
                break;
            }
        }
    }

    private void OnAreaDrag(object[] args)
    {
        if (dragCharacter == null) return;

        Vector2 pos = (Vector2)args[0];
        var worldpos = mainCamera.ScreenToWorldPoint(pos);
        worldpos.z = dragCharacter.Transform.position.z;
        dragCharacter.Transform.position = worldpos;

        if (curGrid != null)
        {
            curGrid.PlacePreview(false);
            curGrid = null;
        }
        var grids = BattleManager.Instance.Grids;
        for (int y = -2; y < 3; y++)
        {
            for (int x = -2; x < 3; x++)
            {
                var item = grids[x + 2, y + 2];
                if (item.Collider.OverlapPoint(worldpos))
                {
                    curGrid = item;
                    curGrid.PlacePreview();
                    break;
                }
            }
        }
    }

    private void OnAreaEndDrag(object[] args)
    {
        if(dragCharacter == null)
        {
            return;
        }
        if (curGrid != null)
        {
            dragCharacter.Grid = curGrid;
        }
        dragCharacter.GotoGrid();
        dragCharacter = null;
    }
    #endregion
}
